<?php
class apply_model extends CI_Model
{
	public $table_name='apply';
	function __construct()
	{
		parent::__construct();
		$config['hostname'] = 'localhost';
		$config['username'] = 'root';
		$config['password'] = '';
		$config['database'] = 'helloword';
		$config['dbdriver'] = 'mysql';
		$config['dbprefix'] = '';
		$config['pconnect'] = TRUE;
		$config['db_debug'] = TRUE;
		$config['cache_on'] = FALSE;
		$config['cachedir'] = '';
		$config['char_set'] = 'utf8';
		$config['dbcollat'] = 'utf8_general_ci';
		$config['swap_pre'] = '';
		$config['autoinit'] = TRUE;
		$config['stricton'] = FALSE;
		//@@@@@@testers: check the database name before start!!
		$this->load->database($config);
	}
	
	
	//full functionality, @@required field name to be matched@@
	function link($helloid)
	{
		$new_link['helloid'] = $helloid;
		$new_link['guestid'] = $this->session->userdata('userid');
		$this->db->where( 'helloid', $new_link['helloid']);
		$this->db->where( 'guestid', $new_link['guestid'] );
		
		$query = $this->db->get($this->table_name);
		
		if($query->num_rows >= 1)
			return false;
		else
			return $this->db->insert($this->table_name, $new_link);
			
	}
	//not yet being used
	function delete($helloid)
	{
		$this->db->where('helloid', $helloid);
		$this->db->where( 'guestid', $this->session->userdata('userid') );
		$this->db->delete($this->table_name);
	}
	
	function get_apply($profile)
	{
		reset($profile);

		while (list($field, $content) = each($profile))
		{
			$this->db->where($field, $content);
		}
		return $this->db->get($this->table_name);
	}
	
	//As the name says, I do not know I ever need it
	function ChangeQureyResultToArray($query)
	{
		if($query->num_rows() > 0)
		{
			foreach ($query->result() as $row)
			{
				$data[] = $row;
			}
			return $data;
		}
	}
	
	// this may no longer be useful, even for test
	function getAll()//For test use Only
	{
		return $this->db->get($this->table_name);
	}
}
?>
